<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    legendname: {
      type: Array,
      default: () => [],
    },
    xAxis: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
      myxAxis: [],
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        console.log("val:", val);
        this.setOptions(val);
      },
    },
    xAxis: {
      deep: true,
      handler(val) {
        this.myxAxis = val;
        this.setOptions(this.chartData);
      },
    },
  },
  mounted() {
    this.myxAxis = this.xAxis;
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(params) {
      console.log('this.legendname:', this.legendname)
      let series = [];
      this.legendname.forEach((item, index) => {
        // console.log('item:',item,params, params[item.id])
        let color = "#3888fa";
        if (index == 1) {
          color = "#E50000";
        } else if (index == 2) {
          color = "#F1C96B";
        }
        let seriesitem = {
          name: item.name,
          itemStyle: {
            normal: {
              color: item.color ? item.color : color,
              lineStyle: {
                color:item.color ? item.color : color,
                width: 2,
              },
            },
          },
          smooth: false,
          type: "line",
          data: params[item.id],
          // animationDuration: 1200,
          // animationEasing: "cubicInOut",
        };
        series.push(seriesitem);
      });
      // 
      // console.log("this.myxAxis:", series);
      this.chart.setOption({
        xAxis: {
          data: this.myxAxis.map(function (str) {
            return str.replace(" ", "\n");
          }),
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: "#333",
            },
            // interval: 0,
          },
          axisLine: {
            //横轴样式
            lineStyle: {
              color: "#333",
            },
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: "#333",
            },
          },
          axisLine: {
            //横轴样式
            lineStyle: {
              color: "#333",
            },
          },
        },
        dataZoom: [
         {
           type: 'inside',
           start: 0,
           end:100
          },
          {
           show: false,
           start: 0,
           end: 100
          }
        ],
        // legend: {
        //   data: this.legendname.map(item=>item.name)
        // },
        series: series,
      });
    },
  },
};
</script>
